package com.tbm.console.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.LocalConstant;
import com.tbm.console.proj.entity.SystemRole;
import com.tbm.console.proj.entity.SystemUser;
import com.tbm.console.proj.service.LocalConstantService;
import com.tbm.console.proj.service.SystemRoleService;
import com.tbm.console.proj.service.SystemUserService;
import com.tbm.console.util.JsonUtils;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SystemUserController extends BaseController {

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private LocalConstantService localConstantService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户管理--用户列表
     * @return
     */
    @RequestMapping("/admin-list-manage")
    public ModelAndView adminListManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);
            viewName = "admin/admin-list-manage";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/admin-add")
    public ModelAndView adminAddManage() {
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){

            PageData pd = this.getPageData();
            List<PageData> roleList = new ArrayList<>();
            List<PageData> groupList = new ArrayList<>();
            List<PageData> levelList = new ArrayList<>();
            //先读取redis
            try{
                String role = stringRedisTemplate.opsForValue().get("role");
                String group = stringRedisTemplate.opsForValue().get("group");
                String powerLevel = stringRedisTemplate.opsForValue().get("level");
                roleList = JsonUtils.jsonToObject(role, List.class);
                groupList = JsonUtils.jsonToObject(group, List.class);
                levelList = JsonUtils.jsonToObject(powerLevel, List.class);
                //表示如果不存在
                if(null==role){
                    roleList = getRole();
                    stringRedisTemplate.opsForValue().set("role",JsonUtils.objectToJson(roleList));
                }
                if(null==group){
                    groupList = getConstantByParam("group-types");
                    stringRedisTemplate.opsForValue().set("group",JsonUtils.objectToJson(groupList));
                }
                if(null==powerLevel){
                    levelList = getConstantByParam("power-level-types");
                    stringRedisTemplate.opsForValue().set("level",JsonUtils.objectToJson(levelList));
                }
            }catch (Exception e){
                //表示如果连不上
                roleList = getRole();
                groupList = getConstantByParam("group-types");
                levelList = getConstantByParam("power-level-types");
            }
            SystemUser id = new SystemUser();
            if(null!=pd.get("id")&&!"".equals(pd.getString("id"))){
                id = systemUserService.findById(Integer.valueOf(pd.getString("id")));
            }
            modelAndView.addObject("user", id);
            modelAndView.addObject("role_list", roleList);
            modelAndView.addObject("group_list", groupList);
            modelAndView.addObject("level_list", levelList);
            viewName = "admin/admin-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
    //获取角色
    private List<PageData> getRole(){
        List<SystemRole> all = systemRoleService.findAll();
        List<PageData> list = new ArrayList<>();
        for(SystemRole systemRole : all){
            PageData pd = new PageData();
            pd.put("key",systemRole.getId()) ;
            pd.put("value",systemRole.getRoleName());

            list.add(pd);
        }
        return list;
    }
    //获取组别
    private List<PageData> getConstantByParam(String key){
        List<PageData> list = new ArrayList<>();
        PageData param = new PageData();
        param.put("constantKey", key);
        param.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        List<LocalConstant> all = localConstantService.findAll(param);
        for(LocalConstant localConstant :all){
            PageData pd = new PageData();
            pd.put("key", localConstant.getConstantValue());
            pd.put("value", localConstant.getConstantValueDesc());
            list.add(pd);
        }
        return list;
    }

}
